package com.yj.suncity.jpush;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.provider.SyncStateContract.Constants;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.yj.suncity.LaunchActivity;
import com.yj.suncity.MainActivity;
import com.yj.suncity.R;
import com.yj.suncity.utis.Utils;

public class NocationService extends Service {
	private static final String NEWS_LIST_ITEM_NAME = "news_name";
	private static final String NEWS_CONTENT_ID = "content_id";
	private static final String REAR_LIST_ITEM_ID = "_id";
	private static final String REAR_LIST_ITEM_NAME = "teach_name";
	private static final String NOTICATION_ITEM_NAME = "notice_name";
	private static final String CONTENT_ID = "content_id";
	private static final int notifiId = 11;
	protected NotificationManager notificationManager;
	private ServiceBroadcastReciver mResiver = new ServiceBroadcastReciver();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(JpushResciver.SERVICE_ACTION);
		registerReceiver(mResiver, intentFilter);

		return super.onStartCommand(intent, flags, startId);
	}

	private void notifyNewMessage(String message) {
		// 如果是设置了不提醒只显示数目的群组(这个是app里保存这个数据的，demo里不做判断)
		// 以及设置了setShowNotificationInbackgroup:false(设为false后，后台时sdk也发送广播)
//			JSONObject jsonObj = new JSONObject(message);
//			String title = jsonObj.getString("title");
//			String content = jsonObj.getString("content");
//			int type = jsonObj.getInt("type");
//			String contentId = jsonObj.getString("contentid");
			Intent resultIntent = new Intent();
//			switch(type){
//			case 0:
//				resultIntent.setClass(getApplicationContext(), StoryActivity.class);
//				break;
//			case 1:
//				resultIntent.setClass(getApplicationContext(), RearingInfoActivity.class);
//				resultIntent.putExtra(REAR_LIST_ITEM_ID, contentId);
//				resultIntent.putExtra(REAR_LIST_ITEM_NAME, content);
//				break;
//			case 2:
//				resultIntent.setClass(getApplicationContext(), SchoolNewsInfoActivity.class);
//				resultIntent.putExtra(NEWS_CONTENT_ID, contentId);
//				resultIntent.putExtra(NEWS_LIST_ITEM_NAME, content);
//				break;
//			case 3:
//				resultIntent.setClass(getApplicationContext(), NoticationInfoActivity.class);
//				resultIntent.putExtra(CONTENT_ID, contentId);
//				resultIntent.putExtra(NOTICATION_ITEM_NAME, content);
//				break;
//			case 4:
//				resultIntent.setClass(getApplicationContext(), FoodInfoActivity.class);
//				resultIntent.putExtra(CONTENT_ID, contentId);
//				Date curDate = new Date(Long.parseLong(content));
//				SimpleDateFormat formatter = new SimpleDateFormat(Constants.TIME_FORMAT);
//				String t = formatter.format(curDate);
//				content = Utils.parseDateToDay(t)+"食谱";
//				break;
//			case 5:
//				resultIntent.setClass(getApplicationContext(), KidsWorkInfoActivity.class);
//				resultIntent.putExtra(CONTENT_ID, contentId);
//				break;
//			}
			resultIntent.setClass(getApplicationContext(), LaunchActivity.class);
			TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
			stackBuilder.addParentStack(MainActivity.class);
			stackBuilder.addNextIntent(resultIntent);
			PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
					.setSmallIcon(R.drawable.app_icon)
					.setContentTitle("通知")
					.setContentText(message)
					.setContentIntent(resultPendingIntent)
					.setWhen(System.currentTimeMillis())
					.setAutoCancel(true);
			
			// 设置状态栏提示
			mBuilder.setTicker("通知"+":"+message);

			Notification notification = mBuilder.build();
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.defaults = Notification.DEFAULT_ALL;
			notificationManager.notify(notifiId, notification);


	}

	public class ServiceBroadcastReciver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (JpushResciver.SERVICE_ACTION.equals(action)) {
				String msg = intent.getStringExtra("msg");
				notifyNewMessage(msg);
			}
		}

	}
}
